package com.wm.task1;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) throws IOException {
        String ip = "192.168.0.196";
        int port = 8888;
        Scanner sc = new Scanner(System.in);
        //D:/java/study/test/download/result.txt
        try (Socket socket = new Socket(ip, port);
             InputStream is = socket.getInputStream();
             InputStreamReader isr = new InputStreamReader(is, "UTF-8");) {
            System.out.print("服务器下发文件，请输入接收保存路径:");
            File file = new File(sc.next());
            FileWriter writer = new FileWriter(file, true);
            writer.write("服务器:\n");
            while (true) {
                int r = isr.read();
                if (r == -1) {
                    break;
                }

                writer.write((char) r);

            }
            if (file.exists() && file.isFile()) {
                System.out.println("文件接收成功!!");
            } else {
                System.out.println("文件接受失败!!");
            }

            writer.close();


        } catch (Exception e) {

        }
    }
}
